<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="" prop="orderId">
              <el-input v-model="queryParams.orderId" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="商品分类" prop="productsCateId">
              <el-input v-model="queryParams.productsCateId" placeholder="请输入商品分类" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="商品ID" prop="productId">
              <el-input v-model="queryParams.productId" placeholder="请输入商品ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm:processInfo:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="processInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="true" label="" align="center" prop="id" />
        <el-table-column label="" align="center" prop="orderId" />
        <el-table-column label="商品分类" align="center" prop="productsCateId" />
        <el-table-column label="商品ID" align="center" prop="productId" />
        <el-table-column label="数量" align="center" prop="amount" />
        <el-table-column label="单价" align="center" prop="price" />
        <el-table-column label="小计" align="center" prop="money" />
        <el-table-column label="库存数量" align="center" prop="stock" />
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="ProcessInfo" lang="ts">
import { listProcessInfo } from '@/api/farm/common/processInfo';
import { ProcessInfoVO } from '@/api/farm/common/processInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const processInfoList = ref<ProcessInfoVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const data = reactive<any>({
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    orderId: undefined,
    productsCateId: undefined,
    productId: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(data);

/** 查询加工详情列表 */
const getList = async () => {
  loading.value = true;
  const res = await listProcessInfo(queryParams.value);
  processInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: ProcessInfoVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'farm/processInfo/export',
    {
      ...queryParams.value
    },
    `processInfo_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
